package com.study.d012Components.listview;

import java.util.Comparator;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
*@description 使用简单数据类型 测试可观察列表的动作
*@author xtayhame
*date 2020年3月13日
*version
*since v1.0
*/
public class ListViewEventWithString extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		ObservableList<String> datas=FXCollections.observableArrayList();
		datas.addAll("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w");

		Button b1  = new Button("点击");
		ListView<String> listView = new ListView<>(datas);
		listView.setPrefSize(200, 300);
		anchorPane.getChildren().addAll(b1,listView);
		AnchorPane.setLeftAnchor(listView, 50.0);
		AnchorPane.setTopAnchor(listView, 50.0);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();

		datas.addListener(new ListChangeListener<String>() {

			@Override
			public void onChanged(javafx.collections.ListChangeListener.Change<? extends String> c) {
				if(c.next()){
					if(c.wasAdded()){
						System.out.println("新增动作");
					}
					if(c.wasPermutated()){
						System.out.println("排序动作");
					}
					if(c.wasRemoved()){
						System.out.println("移除动作");
					}
					if(c.wasReplaced()){
						System.out.println("替换动作");
					}
					if(c.wasUpdated()){
						System.out.println("更新动作");
					}
				}
			}
		});


		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				//datas.set(0, "abcdef");    //新增、移除、替换
				//datas.add("abcdef");   //新增
				//datas.remove(0);   // 移除


				//排序
				datas.sort(new Comparator<String>() {

					@Override
					public int compare(String o1, String o2) {
						// TODO Auto-generated method stub
						return o2.compareTo(o1);
					}
				});
			}
		});
	}

}
